package com.ocm.tourism.web;

import java.util.ArrayList;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ocm.tourism.core.domain.Catalogues;
import com.ocm.tourism.core.domain.CulinaryCulture;
import com.ocm.tourism.core.domain.Hotel;
import com.ocm.tourism.core.service.CataloguesService;
import com.ocm.tourism.core.service.CommentService;
import com.ocm.tourism.web.model.CatelogueResult;
import com.ocm.tourism.web.model.FoodSearchResult;
import com.ocm.tourism.web.model.HotelSearchResult;

@RequestMapping("/attraction")
@Controller
public class TourismAttractionController {

   @Autowired
   CataloguesService cataloguesService;

   @Autowired
   CommentService commentService;

   @RequestMapping(value = "/main")
   public String viewMain(Model uiModel) {
      uiModel.addAttribute("submenuSelected", "touristattraction");
      return "attraction/main";
   }

   @RequestMapping(value = "/content/{attractionType}/{attractionMenu}", method=RequestMethod.GET)
   public String viewContent(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu, Model uiModel) {
      uiModel.addAttribute("attractionType", attractionType);
      uiModel.addAttribute("attractionMenu", attractionMenu);
      uiModel.addAttribute("submenuSelected", "touristattraction");
      if ("share".equalsIgnoreCase(attractionMenu)){
         //commentService.
      }else {
         String cateloguesType = createCateloguesType(attractionType, attractionMenu);
         Catalogues catalogues = this.cataloguesService.findCatalogues(cateloguesType);
         populateEditForm(uiModel, catalogues);
      }
      return "attraction/content";
   }

   @RequestMapping(value = "/content/{attractionType}/{attractionMenu}/light", method=RequestMethod.GET)
   public String viewContentViaAjax(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu, Model uiModel) {
      uiModel.addAttribute("attractionType", attractionType);
      uiModel.addAttribute("attractionMenu", attractionMenu);
      if ("share".equalsIgnoreCase(attractionMenu)){
         return "forward:/comment/"+attractionType+"/"+attractionMenu+"?page=1&size=25";
      }else {
         String cateloguesType = createCateloguesType(attractionType, attractionMenu);
         Catalogues catalogues = this.cataloguesService.findCatalogues(cateloguesType);
         populateEditForm(uiModel, catalogues);
      }
      return "attraction/content/"+attractionMenu+"/light";
   }

   @RequestMapping(value = "/update/{attractionType}/{attractionMenu}/light", method=RequestMethod.GET)
   public String viewUpdateContent(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu, Model uiModel) {
      uiModel.addAttribute("attractionType", attractionType);
      uiModel.addAttribute("attractionMenu", attractionMenu);
      String cateloguesType = createCateloguesType(attractionType, attractionMenu);
      Catalogues catalogues = this.cataloguesService.findCatalogues(cateloguesType);
      populateEditForm(uiModel, catalogues);
      if ("hotel".equalsIgnoreCase(attractionMenu.trim())){
         uiModel.addAttribute("hotel", new Hotel());
         uiModel.addAttribute("hotelResults", new Hotel());
         uiModel.addAttribute("hotels", new ArrayList<Hotel>());
      }else if ("foodspecial".equalsIgnoreCase(attractionMenu.trim())){
         uiModel.addAttribute("foodspecial", new Catalogues());
         uiModel.addAttribute("foodspecials", cataloguesService.findFoodSpecials(0, 50));
      }else if ("food".equalsIgnoreCase(attractionMenu.trim())){
         uiModel.addAttribute("food", new CulinaryCulture());
         uiModel.addAttribute("foodResults", new CulinaryCulture());
         uiModel.addAttribute("foods", new ArrayList<CulinaryCulture>());
      }
      uiModel.addAttribute("cateloguesType",cateloguesType);
      return "attraction/form/"+attractionMenu;
   }

   @RequestMapping(value = "/update/{attractionType}/{attractionMenu}", method=RequestMethod.POST)
   public String updateContent(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu, @Valid Catalogues catalogues, BindingResult bindingResult,Model uiModel) {
      if (bindingResult.hasErrors()) {
         populateEditForm(uiModel, catalogues);
         return "attraction/form/"+attractionMenu;
     }
     uiModel.asMap().clear();
     catalogues.setCataloguesType(createCateloguesType(attractionType, attractionMenu));
     cataloguesService.saveCatalogues(catalogues);
     return "redirect:/attraction/content/"+attractionType+"/"+attractionMenu;
   }

   @RequestMapping(value = "/linkhotel/{attractionType}/{attractionMenu}", method=RequestMethod.POST)
   public String linkhotel(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu,@ModelAttribute("hotelResults")HotelSearchResult hotelSearchResult, BindingResult bindingResult, Model uiModel) {
      uiModel.addAttribute("attractionType", attractionType);
      uiModel.addAttribute("attractionMenu", attractionMenu);
      String cateloguesType = createCateloguesType(attractionType, attractionMenu);
      Catalogues catalogues = this.cataloguesService.findCatalogues(cateloguesType);
      if (catalogues == null){
         catalogues = new Catalogues();
         catalogues.setName(cateloguesType);
         catalogues.setCataloguesType(cateloguesType);
      }
      cataloguesService.linkHotels(catalogues, hotelSearchResult.getHotelResults());
      populateEditForm(uiModel, catalogues);
      return "attraction/content/"+attractionMenu+"/light";
   }

   @RequestMapping(value = "/linkFood/{attractionType}/{attractionMenu}", method=RequestMethod.POST)
   public String linkFood(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu,@ModelAttribute("foodResults")FoodSearchResult foodSearchResult, BindingResult bindingResult, Model uiModel) {
      uiModel.addAttribute("attractionType", attractionType);
      uiModel.addAttribute("attractionMenu", attractionMenu);
      String cateloguesType = createCateloguesType(attractionType, attractionMenu);
      Catalogues catalogues = this.cataloguesService.findCatalogues(cateloguesType);
      if (catalogues == null){
         catalogues = new Catalogues();
         catalogues.setName(cateloguesType);
         catalogues.setCataloguesType(cateloguesType);
      }
      cataloguesService.linkFoods(catalogues, foodSearchResult.getFoodResults());
      populateEditForm(uiModel, catalogues);
      return "attraction/content/"+attractionMenu+"/light";
   }
   
   @RequestMapping(value = "/link/{attractionType}/{attractionMenu}", method=RequestMethod.POST)
   public String link(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu,@ModelAttribute("foodspecials")CatelogueResult catelogueResult, BindingResult bindingResult, Model uiModel) {
      uiModel.addAttribute("attractionType", attractionType);
      uiModel.addAttribute("attractionMenu", attractionMenu);
      String cateloguesType = createCateloguesType(attractionType, attractionMenu);
      Catalogues catalogues = this.cataloguesService.findCatalogues(cateloguesType);
      if (catalogues == null){
         catalogues = new Catalogues();
         catalogues.setName(cateloguesType);
         catalogues.setCataloguesType(cateloguesType);
      }
      cataloguesService.linkCatalogues(catalogues, catelogueResult.getCategolues());
      populateEditForm(uiModel, catalogues);
      return "attraction/content/"+attractionMenu+"/light";
   }
   
   private String createCateloguesType(String attractionType, String attractionMenu){
      return attractionType+"_"+attractionMenu;
   }

   private void populateEditForm(Model uiModel, Catalogues catalogues) {
      uiModel.addAttribute("catalogues", catalogues == null ? new Catalogues() : catalogues);
  }
}
